//
//  LoginViewController.m
//  Fengshui
//
//  Created by are on 13-5-31.
//
//

#import "LoginViewController.h"
#import "ASIHTTPRequest.h"
#import "RegisterViewController.h"
#import "ASIFormDataRequest.h"
@interface LoginViewController ()

@end

@implementation LoginViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (BOOL)checkTel:(NSString *)str
{
    NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    //    Dlog(@"phoneTest is %@",phoneTest);
    return [phoneTest evaluateWithObject:str];
}
-(IBAction)registePhone:(id)sender
{
    RegisterViewController *viewController = [[RegisterViewController alloc] initWithNibName:@"RegisterViewController" bundle:nil];
    [self.navigationController pushViewController:viewController animated:YES];
    [viewController release];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.resendNum = 45;
    self.yanzhengmaBtn.titleLabel.textAlignment = 1;
    // Do any additional setup after loading the view from its nib.
}
-(void)daojishi
{
    self.resendNum = self.resendNum - 1;
    if (self.resendNum == 0) {

        [self.time invalidate];
        self.yanzhengmaBtn.enabled = YES;
        self.yanzhengmaBtn.titleLabel.text = @"重新发送";
        self.resendNum = 45;
        
    }
    else
    {
          self.yanzhengmaBtn.titleLabel.text =  [NSString stringWithFormat:@"重新发送(%d)",self.resendNum];
    }
  
    
}
-(IBAction)registerBtnAction:(id)sender
{
    //获取uphone_num=13911762456用户的信息
    //http://fengshui22.sinaapp.com/ios/SmsVerify.php?uphone_num=13911762456
    if ([self.userTextfield.text isEqualToString:@""]) {

        return;
        
    }else

    {
        if([self checkTel:self.userTextfield.text])
        {
            self.yanzhengmaBtn.enabled = NO;
            NSString *description = [NSString stringWithFormat:@"重新发送(%d)",self.resendNum];
            self.yanzhengmaBtn.titleLabel.text = description;
            self.time = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(daojishi) userInfo:nil repeats:YES];

            NSString *string = [NSString stringWithFormat:@"http://fengshui22.sinaapp.com/ios/SmsVerify.php?uphone_num=%@",self.userTextfield.text];
            NSURL *url = [NSURL URLWithString:string];
            ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
            [request startSynchronous];
            NSError *error = [request error];
            if (!error) {

                NSString *string = [request responseString];
                if ([string isEqualToString:@"wrong_format"]) {

                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"格式错误" message:nil
                                                                   delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                    [alert show];
                    [alert release];

                }
                else if ([string isEqualToString:@"have_registered_before"])
                {
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"已经注册过了" message:nil
                                                                   delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                    [alert show];
                    [alert release];
                    
                }
                else
                {
                    NSString *yZMstr = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
                    self.yanzhengMa = [NSString stringWithString:yZMstr];
                }
            }
            
        }
        else
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"格式错误" message:nil
                                                           delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
            [alert release];
        }
    }
    
}
-(IBAction)bangdinShoujihao:(id)sender
{
    if ([self.yanzhengmaTestfield.text isEqualToString:@""]) {

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"填写验证码" message:nil
                                                       delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
        
    }
    else
    {
        NSString *string = [self.yanzhengmaTestfield.text encodeMd5];
        NSLog(@"%@",string);
        if ([string isEqualToString:self.yanzhengMa]) {
            
            NSString *string = @"http://fengshui22.sinaapp.com/ios/User.php";
            NSURL *url = [NSURL URLWithString:string];
            ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:url];
            [request setPostValue:@"phone" forKey:@"submit_type"];
            [request setPostValue:self.userTextfield.text forKey:@"uphone_num"];
            [request setDelegate:self];
            [request startSynchronous];
            
            NSLog(@"%@",request.responseString);
            NSError *error = [request error];
            if (!error) {

                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"绑定成功!" message:nil
                                                               delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                [alert show];
                [alert release];
                
                NSString *string = [NSString stringWithString:request.responseString];
                NSDictionary *dic = [string JSONValue];
                NSString *uid = [dic objectForKey:@"uid"];
                NSString *uphone_num = [dic objectForKey:@"uphone_num"];
//                "uid": "2",
//                "uname": "胡亮",
//                "uphone_num": "13911762456",
//                "usex": "1",
//                "uborth_date": "320833500",
//                "unow_area": "北京市丰台区",
//                "uborth_area": "北京市朝阳区"
                NSDictionary *user_dic = [NSDictionary dictionaryWithObjectsAndKeys:uid,@"uid",uphone_num,@"uphone_num",nil];
                [[NSUserDefaults standardUserDefaults] setObject:user_dic forKey:@"userIphoneInfo"];
                [[NSUserDefaults standardUserDefaults] synchronize];

            
            }
            
        }
        else
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"验证码错误请重新输入或获取" message:nil
                                                           delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
            [alert release];
        }
        
    }
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
